/*
 *    Copyright (c) 2018-2025, lengleng All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the following disclaimer.
 * Redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution.
 * Neither the name of the pig4cloud.com developer nor the names of its
 * contributors may be used to endorse or promote products derived from
 * this software without specific prior written permission.
 * Author: lengleng (wangiegie@gmail.com)
 */
package com.excesys.exsecs.om.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.excesys.exsecs.common.core.util.R;
import com.excesys.exsecs.om.dto.PlanDto;
import com.excesys.exsecs.om.entity.OmICheckPlan;
import com.excesys.exsecs.om.entity.OmICheckPlanTime;
import com.excesys.exsecs.om.entity.OmICheckUserRel;
import com.excesys.exsecs.om.mapper.OmICheckPlanMapper;
import com.excesys.exsecs.om.mapper.OmICheckPlanTimeMapper;
import com.excesys.exsecs.om.mapper.OmICheckUserRelMapper;
import com.excesys.exsecs.om.mapper.SequenceMapper;
import com.excesys.exsecs.om.service.OmICheckPlanService;
import com.excesys.exsecs.om.utils.OrderNoUtils;
import com.excesys.exsecs.om.utils.PlanToSheetUtils;
import com.excesys.exsecs.om.vo.PlanVo;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;
import java.util.Optional;

/**
 * 巡检计划表
 *
 * @author weiyj
 * @date 2023-03-01 14:10:58
 */
@Service
@RequiredArgsConstructor
public class OmICheckPlanServiceImpl extends ServiceImpl<OmICheckPlanMapper, OmICheckPlan>
		implements OmICheckPlanService {

	private final OmICheckPlanTimeMapper omICheckPlanTimeMapper;

	private final OmICheckUserRelMapper omICheckUserRelMapper;

	private final SequenceMapper sequenceMapper;

	private final OrderNoUtils orderNoUtils;

	private final PlanToSheetUtils planToSheetUtils;

	@Override
	@Transactional(rollbackFor = Exception.class)
	public Boolean savePlan(PlanDto planDto) {

		OmICheckPlan omICheckPlan = new OmICheckPlan();
		BeanUtils.copyProperties(planDto, omICheckPlan);

		int eventSeq = sequenceMapper.getSeqNextVal("planseq");
		omICheckPlan.setFrequencyNum(1);
		omICheckPlan.setPlanNo(orderNoUtils.getOrderNumber("Plan", eventSeq));
		baseMapper.insert(omICheckPlan);
		planDto.setPlId(omICheckPlan.getPlId());
		planDto.setPlanNo(omICheckPlan.getPlanNo());
		// 保存巡检计划执行时间信息
		Optional.ofNullable(planDto.getRelList()).ifPresent(relLists -> {
			relLists.stream().map(omICheckPlanTime -> {
				omICheckPlanTime.setPlanId(omICheckPlan.getPlId());
				return omICheckPlanTime;
			}).forEach(omICheckPlanTimeMapper::insert);
		});
		// planToSheetUtils.generatorTaskToday(planDto);

		// 保存巡检计划巡检人信息
		Optional.ofNullable(planDto.getUserRelList()).ifPresent(userRelLists -> {
			userRelLists.stream().map(omICheckUserRel -> {
				omICheckUserRel.setPlanNo(omICheckPlan.getPlanNo());
				return omICheckUserRel;
			}).forEach(omICheckUserRelMapper::insert);
		});
		planToSheetUtils.generatorTaskToday(planDto);
		return Boolean.TRUE;
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public R<Boolean> updatePlan(PlanDto planDto) {

		OmICheckPlan omICheckPlan = new OmICheckPlan();
		BeanUtils.copyProperties(planDto, omICheckPlan);
		omICheckPlan.setUpdateTime(LocalDateTime.now());
		this.updateById(omICheckPlan);
		omICheckPlanTimeMapper
				.delete(Wrappers.<OmICheckPlanTime>lambdaQuery().eq(OmICheckPlanTime::getPlanId, planDto.getPlId()));
		// 保存用户岗位信息
		Optional.ofNullable(planDto.getRelList()).ifPresent(relLists -> {
			relLists.stream().map(OmICheckPlanTime -> {
				OmICheckPlanTime.setPlanId(omICheckPlan.getPlId());
				return OmICheckPlanTime;
			}).forEach(omICheckPlanTimeMapper::insert);
		});

		omICheckUserRelMapper
				.delete(Wrappers.<OmICheckUserRel>lambdaQuery().eq(OmICheckUserRel::getPlanNo, planDto.getPlanNo()));
		// 保存巡检计划巡检人信息
		Optional.ofNullable(planDto.getUserRelList()).ifPresent(userRelLists -> {
			userRelLists.stream().map(omICheckUserRel -> {
				omICheckUserRel.setPlanNo(planDto.getPlanNo());
				return omICheckUserRel;
			}).forEach(omICheckUserRelMapper::insert);
		});
		return R.ok();
	}

	@Override
	public Boolean removePlanById(OmICheckPlan omICheckPlan) {
		omICheckPlanTimeMapper.delete(
				Wrappers.<OmICheckPlanTime>lambdaQuery().eq(OmICheckPlanTime::getPlanId, omICheckPlan.getPlId()));
		this.removeById(omICheckPlan.getPlId());
		return Boolean.TRUE;
	}

	/**
	 * 分页查询计划信息（含有时间信息）
	 * @param page 分页对象
	 * @param planDto 参数列表
	 * @return
	 */
	@Override
	public IPage<PlanVo> getPlanWithRelPage(Page page, PlanDto planDto) {
		return baseMapper.getPlanWithRelPage(page, planDto);
	}

	/**
	 * 分页查询计划信息（含有时间信息）
	 * @param planDto 参数列表
	 * @return
	 */
	@Override
	public List<PlanVo> getPlanWithRelList(PlanDto planDto) {
		return baseMapper.getPlanWithRelList(planDto);
	}

	@Override
	public Map<String, String> getCheckPlanFinishCountByTimeType(String timeType, String statusType) {
		return baseMapper.getCheckPlanFinishCountByTimeType(timeType, statusType);
	}

	@Override
	public List<OmICheckPlan> getPlanList() {
		return baseMapper.getPlanList();
	}

}
